package com.wuyan.locationdog.ui.device

import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.ext.loadImageCircleCar
import com.arcsoft.commonbase.ext.textStringTrim
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.BarUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.adapter.ServiceDetailListAdapter
import com.wuyan.locationdog.databinding.ServiceDetailActivityBinding
import com.wuyan.locationdog.help.DeviceHelp
import com.wuyan.locationdog.view.PayPopup
import com.wuyan.locationdog.viewmodel.MyViewModel


class ServiceDetailActivity : BaseBindingActivity<ServiceDetailActivityBinding>() {
    private lateinit var payType: String
    private var commodityId: Int = -1
    private val mViewModel: MyViewModel by viewModels()

    private val mAdapter by lazy { ServiceDetailListAdapter(R.layout.item_service_detail_list, null) }

    override fun initData() {
        BarUtils.transparentStatusBar(this)

        commodityId = intent.extras?.getInt("commodityId") ?: -1


        binding.apply {
            setTitleText("流量套餐购买", bgTransparent = true)

            imei.text = "设备ID：" + DeviceHelp.selectDeviceBeanContent.imei
            deviceName.text = DeviceHelp.getDeviceName(DeviceHelp.selectDeviceBeanContent)
            deviceAvatarUrl.loadImageCircleCar(DeviceHelp.selectDeviceBeanContent.dog?.dogImg)

            recyclerView.apply {
                layoutManager = LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false)
                adapter = mAdapter.apply {

                    addChildClickViewIds(R.id.itemLayout)
                    setOnItemChildClickListener { adapter, view, position ->
                        binding.apply {
                            packageDesc.text = data[position].packageDesc
                        }
                        data.find { it.isSelect }?.isSelect = false
                        data[position].isSelect = true
                        notifyDataSetChanged()
                    }
                }
            }
            btnSure.setOnClickListener {
                val bean = mAdapter.data.find { it.isSelect }

                val payPopup = PayPopup(mActivity, bean!!.price) {
                    payType = it
                    val map = HashMap<String, Any>()
                    map["imei"] = DeviceHelp.selectDeviceBeanContent.imei
                    map["payType"] = payType
                    map["specId"] = bean.specId

                    mViewModel.serviceAdd(map)
                }.showPopupWindow()

            }

        }
    }

    override fun loadNetData() {
        super.loadNetData()
        val map = HashMap<String, Any>()
        mViewModel.serviceRechargeDetail(map)
//        if (commodityId == -1) {
//            val map = HashMap<String, Any>()
//            mViewModel.serviceRechargeDetail(map)
//        } else {
//            val map = HashMap<String, Any>()
//            map["commodityId"] = commodityId
//            mViewModel.serviceDetail(map)
//        }

    }


    override fun observe() {
        super.observe()

        mViewModel.serviceDetail.observe(this) {
//            setTitleText(it.content.commodityName, bgTransparent = true)
            binding.apply {
                it.content.commoditySpecList.first().let {
                    packageDesc.text = it.packageDesc
                    it.isSelect = true
                }
                purchaseDesc.text = it.content.purchaseDesc
            }
            mAdapter.setNewInstance(it.content.commoditySpecList.toMutableList())
        }

        mViewModel.shareUrlBean.observe(this) {
            val map = HashMap<String, Any>()
            map["orderId"] = it.content
            map["payType"] = payType
            mViewModel.pay(map)
        }
        mViewModel.success.observe(this) {
            DeviceHelp.addDeviceName = binding.deviceName.textStringTrim()
            ActivityUtils.startActivity(AddSuccessActivity::class.java)
        }

    }

}